/**
 * @fileoverview This is a file where undefinedVars checks are disabled.
 * @suppress {undefinedVars}
 */
// the above is for the Google Closure Compiler ant task


$(document).ready(function() {

    getReplayGames();
    $("#replayAutoButton").click(replayAuto);
    $("#replayManualButton").click(replayManual);

    $("#logOutButton").click(function() {
//        $.get("logOut", {"id":localStorage.getItem("id")},
        $.get("logOut", {"token":localStorage.getItem("token")},
            function() {
				localStorage.removeItem("id");
                localStorage.removeItem("token");
                window.location.replace("login.jsp");
            });
    });

    $("#scoresButton").click(function() {
        document.location.replace("highScoresTemp.jsp?id=" + localStorage.getItem("id"));
    });

    $("#lobbyButton").click(function() {
//        document.location.replace("lobby.jsp?id=" + localStorage.getItem("id"));
        document.location.replace("lobby.jsp?token=" + localStorage.getItem("token"));
    });

    $("#gameButton").click(function() {
        document.location.replace("game.jsp?id=" + localStorage.getItem("id"));
    });
});

function getReplayGames() {
    // get replayable games from localstorage

    if (games == null) {return}

    var games = JSON.parse(localStorage.getItem("PlayersInfo"));
    for (var i = 0; i < games.length; i++) {
        var game = games[i];
        var username = localStorage.getItem("username");
        // display only the games that were played under your username
        if (username === game.username) {
            $("#chooseReplay").prepend("<input type='radio' name='game' value='" + i +
                "' class='replayRadio'><span>  " + game.date + "\t" + game.opponent + "</span></input><br />");
        }
    }
}

function replayAuto() {
    //replay the game automatically from start to finish without user intervention

    var gameIndex = $("#chooseReplay > input:checked").attr("value");
    if (gameIndex == undefined) {
        alert("Palun vali mäng, mida soovid vaadata.");
        return;
    }
    localStorage.setItem("replayIndex", gameIndex);
    document.location.replace("replayAuto.jsp");
}

function replayManual() {
    var gameIndex = $("#chooseReplay > input:checked").attr("value");
    if (gameIndex == undefined) {
        alert("Palun vali mäng, mida soovid vaadata.");
        return;
    }
    localStorage.setItem("replayIndex", gameIndex);
    document.location.replace("replayManual.jsp");
}